export function urlSearchParse(urlSearch: string, transform: {[key: string]: (val: any) => any} = {}) {
    let res: any = {};
    urlSearch.replace('?','').split('&&')
    .map(item => item.split('='))
    .forEach(item => {
        if(!item[0]) return;
        if(transform[item[0]]) {
            res[item[0]] = transform[item[0]](decodeURIComponent(item[1]));
        } else {
            res[item[0]] = decodeURIComponent(item[1]);
        }
    })
    return res;
}

export function urlSearchSringify(searchObj: {[key: string]: any}, transform: {[key: string]: (val: any) => any} = {}) {
    let arr: string[] = [];
    for(let key in searchObj) {
        if(transform[key]) {
            let value = transform[key](searchObj[key]);
            if(value !== undefined) {

            }
        } else {
            if(searchObj[key] !== undefined) {
                try {
                    arr.push(`${key}=${encodeURIComponent(searchObj[key].toString())}`);
                } catch (error) {
                    
                }
            }
        }
    }
    if(arr.length === 0) {
        return '';
    } else {
        return '?'+arr.join('&&');
    }
}

export function removeUrlDefaultValue(searchParams: {[key: string]: any}, defaultValue: {[key: string]: any}) {
    let obj: any = {};
    for(let key in searchParams) {
        if(defaultValue.hasOwnProperty(key) && String(searchParams[key]) === String(defaultValue[key])) {

        } else{
            obj[key] = searchParams[key];
        }
    }
    return obj;
}